﻿//=============================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 作 者：江怀玉
// 日 期：2011-10-11
// 描 述：页面基类，所有的aspx页面继承自基类
//        页面基类提供常用的方法和常用的用户信息
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/10/11   江怀玉           
//=============================================================

using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.Model;
using iSun.Entity;
using iSun.BLL.Base;
namespace iSun.Web.UI.Controls
{
    public class   TreeListPage: ListPage
    {
        /// <summary>
        /// 定义实体ID
        /// </summary>
        public override string EntityId
        {
            get
            {
                BaseEntity instance = iSun.Common.Reflection.CreateEntitysInstance(this.EntityName) as BaseEntity;
                if (instance != null)
                {
                    return instance.GetTableId();
                }
                return "ListId";
            }
        }
 
        //部门类型 0:内部组组织架构 1:外部组织架构
        public int DeptType
        {
            get;
            set;

        }

        IList<DataListTemplate> list;
        private string GetDataQueryFields()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
            }
            if (list == null)
            {
                return "";
            }
            var _list = list.Where(p => p.IsQuery == "Y");
            string _field = "";
            foreach (var item in _list)
            {
                _field = _field + item.Field + ",";
            }

            return _field == string.Empty ? "" : _field.Remove(_field.Length - 1, 1);
        }


        private void SetTreeControl(System.Web.UI.Control ctrl)
        {
            foreach (System.Web.UI.Control c in ctrl.Controls)
            {
                if (c is TreeView)
                {
                    var tv1 = c as TreeView;
                    if (!string.IsNullOrEmpty(MasterPara))
                    {


                        string[] paras = MasterPara.Split('|');
                        if (paras.Length > 4)
                        {
                            tv1.TableName = paras[0];
                            tv1.Caption = paras[1];
                            tv1.Key = paras[2];
                            tv1.ParentKey = paras[3];
                            tv1.path = paras[4];
                            tv1.Condition = getRoleCondition();

                        }

                        if (paras.Length > 5 && tv1 != null)
                        {
                            tv1.AllowCheck = bool.Parse(paras[5].Trim());
                        }



                    }

                    if (GetStringFromQueryString("DeptType", "") != "")
                    {
                        tv1.IsOutDepartment = GetStringFromQueryString("DeptType", "") == "0" ? 0 : 1;
                    }
                    else
                    {
                        if (Session["IsOutSite"] != null)
                        {
                            tv1.IsOutDepartment = (bool)Session["IsOutSite"] == true ? 1 : 0;
                        }
                    }
                    tv1.userID = User.EmpID;
                }
                else
                {
                    SetTreeControl(c);
                }
            }
        }
        protected override void OnLoad(System.EventArgs e)
        {
            SetTreeControl(this.Page);
            iSun.BLL.Base.BaseBussnies bll = new BLL.Base.BaseBussnies();
            var list = bll.GetEntitysByCondition<iSun.Model.Entity>("EntityName='" + this.EntityName + "'");
            iSun.Model.Entity entity = list != null ? list.FirstOrDefault() : null;
            if (entity != null)
            {
                _width = entity.EditPageWidth;
                _height = entity.EditPageHeight;
            }
            // bll.GetEntitys
            // SetButtons();
            base.OnLoad(e);
        }


     





        /// <summary>
        /// 获取角色数据权限信息
        /// </summary>
        /// <returns></returns>
        public string getRoleCondition()
        {
            IList<Entity.SysRoleFunction> list = (new iSun.BLL.BUser()).GetRoleFunction(this.UserId, this.FunctionID);
            StringBuilder strCondition = new StringBuilder("");

            if (list == null)
            {
                return "";
            }
            foreach (var entity in list)
            {
                switch (entity.OperateRangeType.Trim())
                {
                    ///只能查看本人数据
                    case "0":
                        if (strCondition.ToString() != "")
                        {
                            strCondition.Append(" OR ");
                        }
                        strCondition.Append(string.Format(" AddUser='{0}'", this.UserId));
                        break;
                    ///可以看本部门的
                    case "1":
                        if (this.EntityName == "V_CrmCustomer")
                        {
                            strCondition.Append(string.Format(" AreaId in  (select  VALUE  from  fn_Split((select DEPTIDS  from dbo.V_UserEmp  where userid='{0}'),',')) and IsPrevate='0'", this.UserId));
                        }
                        else
                        {
                            if (strCondition.ToString() != "")
                            {
                                strCondition.Append(" OR ");
                            }
                            strCondition.Append(string.Format(" AddUser in (select * from dbo.fn_GetUserDeptUsers('{0}'))", this.UserId));
                        }
                        break;
                    // 全部数据
                    case "2":
                        break;
                    // 自定义条件数据
                    case "3":
                        //strCondition.Append(string.Format(" AddUser in (select * from dbo.fn_GetUserDeptUsers('{0}'))", this.UserId));
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
            }


            return string.IsNullOrEmpty(strCondition.ToString()) ? "" : "(" + strCondition.ToString() + ")";
            //this.UserId
        }
    }

    
}
